﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using DBUtility;
using System.Text;
public partial class Promotion_Promotion4Edit : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }

    #region 属性
    private string Code
    {
        get
        {
            if (Request.QueryString["code"] != null)
            {
                return Request.QueryString["code"].ToString();
            }
            else
            {
                return "";
            }
        }
    }

    private Model.CXFAD8 Property
    {
        get
        {
            Model.CXFAD8 property = null;
            if (ViewState["cxfad8"] != null)
            {
                property = (Model.CXFAD8)ViewState["cxfad8"];
            }
            return property;
        }
        set
        {
            ViewState["cxfad8"] = value;
        }
    }
    private bool IsAdd
    {
        get
        {
            return Convert.ToBoolean(ViewState["IsAdd"]);
        }
        set
        {
            ViewState["IsAdd"] = value;
        }

    }
    private int Status
    {
        get
        {
            if (Request.QueryString["status"] != null)
            {
                return int.Parse(Request.QueryString["status"]);
            }
            else
            {
                return 0;
            }
        }
    }
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //绑定dropdownlist
            ddl_Type.DataSource = new BLL.SHANGPIN().GetSPCOMMODITY_TYPEList2();
            ddl_Type.DataBind();
            ddl_PinPai.DataSource = new BLL.PINPAI().GetPinPai();
            ddl_PinPai.DataBind();

            if (this.Code == "")
            {

                this.lbl_Promotion.Text = BLL.Common.GetDM("300200102");
                BLL.Common.ADDLSH("300200102");
                string start = DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-"
                            + DateTime.Now.Day.ToString();
                string end = (DateTime.Now.Month.ToString() == "12" ? Convert.ToString(Convert.ToInt32(DateTime.Now.Year) + 1) : DateTime.Now.Year.ToString()) + "-" + (DateTime.Now.Month.ToString() == "12" ? "01" : Convert.ToString(Convert.ToInt32(DateTime.Now.Month) + 1)) + "-" + (DateTime.Now.Day.ToString());
                this.Text1.Value = start;
                this.Text2.Value = end;

                this.IsAdd = true;
                btnAddOrUpdate.Text = "添加";
                idShow.Visible = false;
            }
            else
            {
                idTitle.InnerText = "修改优惠活动";
                Model.CXFAD8 cxfad8 = new BLL.CXFAD8().GetModel(this.Code);
                lbl_Promotion.Text = cxfad8.DJBH;
                txt_ActiveName.Text = cxfad8.HDMC;
                this.Text1.Value = cxfad8.RQ_S.ToShortDateString();
                this.Text2.Value = cxfad8.RQ_E.ToShortDateString();
                ddl_Range.SelectedIndex = int.Parse(cxfad8.BYZD3.ToString()) - 1;
                ddl_Way.SelectedIndex = int.Parse(cxfad8.BYZD4.ToString()) - 1;
                ddl_Type.SelectedValue = cxfad8.BYZD1.ToString();
                chkZK.Checked = cxfad8.BYZD8 == 1 ? true : false;
                ddl_PinPai.SelectedValue = cxfad8.BYZD9;
                txt_ZK.Text = cxfad8.BYZD5.ToString("0.00");
                txt_JE.Text = cxfad8.BYZD6.ToString("0.00");
                txt_SL.Text = cxfad8.BYZD7.ToString();
                Tbx_Remark.Value = cxfad8.BZ;
                this.IsAdd = false;
                btnAddOrUpdate.Text = "更新";

                if (this.Status == 2)
                {
                    btnAddOrUpdate.Visible = true;
                    btn_SubPro.Visible = true;
                    btn_SubGift.Visible = true;
                }
                else
                {
                    btnAddOrUpdate.Visible = false;
                    btn_SubPro.Visible = false;
                    btn_SubGift.Visible = false;
                }

            }

            BindDrpList();
        }

    }

    #region 绑定GridView
    protected void BindProduct(int type, int stop, int up, string pinpai, string gift)
    {//绑定商品选择列表
        DataTable shangpin = new BLL.CXFAD8().GetProductByParams(type, stop, up, pinpai, gift);
        this.grdProduct.DataSource = shangpin;
        this.grdProduct.DataBind();
    }
    protected void BindGift(int type, int stop, int up, string pinpai, string gift)
    {//绑定礼品选择列表
        DataTable gifts = new BLL.CXFAD8().GetProductByParams(type, stop, up, pinpai, gift);
        this.grdGift.DataSource = gifts;
        this.grdGift.DataBind();
    }
    protected void BindCXFADMX1()
    {//绑定指定范围的商品列表
        this.grdShowProduct.DataSource = new BLL.CXFAD8MX1().GetList(lbl_Promotion.Text);
        this.grdShowProduct.DataBind();

    }
    protected void BindCXFADMX2()
    {
        this.grdShowGift.DataSource = new BLL.CXFAD8MX2().GetList(lbl_Promotion.Text);
        this.grdShowGift.DataBind();
    }
    #endregion

    #region 拼商品，礼品ID字符串
    protected void chk_CheckedChanged(object sender, EventArgs e)
    {
        foreach (GridViewRow row in grdProduct.Rows)
        {//拼SPDM字符串

            if (((CheckBox)row.Cells[0].FindControl("chk")).Checked)
            {
                string id = grdProduct.DataKeys[row.RowIndex].Value.ToString();
                if (!hd_SPDMS.Value.Contains(id))
                {
                    hd_SPDMS.Value += id + ",";
                }
            }
        }
    }
    protected void chkGift_CheckedChanged(object sender, EventArgs e)
    {
        foreach (GridViewRow row in grdGift.Rows)
        {//拼GIFT字符串

            if (((CheckBox)row.Cells[0].FindControl("chkGift")).Checked)
            {
                string id = grdGift.DataKeys[row.RowIndex].Value.ToString();
                if (!hd_GIFTS.Value.Contains(id))
                {
                    hd_GIFTS.Value += id + ",";
                }
            }
        }

    }
    #endregion

    #region 分页
    protected void grdProduct_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdProduct.PageIndex = e.NewPageIndex;
        BindProduct(-1, 0, 1, "-1", "-1");
    }
    protected void grdShowProduct_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdShowProduct.PageIndex = e.NewPageIndex;
        BindCXFADMX1();
    }
    protected void grdGift_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdGift.PageIndex = e.NewPageIndex;
        BindGift(-1, 0, 1, "-1", "1");
    }
    protected void grdShowGift_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdShowProduct.PageIndex = e.NewPageIndex;
        BindCXFADMX2();
    }
    #endregion

    #region DropdownList事件
    protected void ddl_Range_SelectedIndexChanged(object sender, EventArgs e)
    {//联动和显示
        BindDrpList();
    }
    protected void ddl_Way_SelectedIndexChanged(object sender, EventArgs e)
    {//联动和显示
        BindDrpList();
    }
    #endregion

    #region 绑定DROPDOWNLIST
    protected void BindDrpList()
    {
        #region 显示控制
        ddl_Type.Visible = false;
        ddl_PinPai.Visible = false;

        idSel.Visible = false;
        idselGift.Visible = false;
        idShow.Visible = false;
        idShowGift.Visible = false;

        txt_JE.Enabled = false;
        txt_SL.Enabled = false;
        txt_ZK.Enabled = false;
        //chkZK.Enabled = false;
        switch (ddl_Range.SelectedValue)
        {
            //优惠范围决定的显示
            case "2":
                ddl_Type.Visible = true;
                break;
            case "3":
                idSel.Visible = true;
                idShow.Visible = true;
                BindProduct(-1, 0, 1, "-1", "-1");
                BindCXFADMX1();
                break;
            case "4":
                ddl_PinPai.Visible = true;
                break;
            default:
                break;
        }
        switch (ddl_Way.SelectedValue)
        {
            //优惠方式决定的显示
            case "1":
                txt_ZK.Enabled = true;
                txt_JE.Text = String.Empty;
                txt_SL.Text = String.Empty;
                break;
            case "2":
                txt_JE.Enabled = true;
                //chkZK.Enabled = true;
                txt_ZK.Text = String.Empty;
                txt_SL.Text = String.Empty;
                break;
            case "3":
                txt_SL.Enabled = true;
                txt_JE.Text = String.Empty;
                txt_ZK.Text = String.Empty;
                idselGift.Visible = true;
                idShowGift.Visible = true;
                BindGift(-1, 0, 1, "-1", "1");
                BindCXFADMX2();
                break;
            default:
                break;
        }
        #endregion
    }
    #endregion

    protected void btnAddOrUpdate_Click(object sender, EventArgs e)
    {
        SubmitData();
        Response.Redirect("Promotion4List.aspx", true);


    }
    //提交数据
    private void SubmitData()
    {
        #region 输入检查
        if (this.txt_ActiveName.Text.Trim() == string.Empty)
        { Share.Alert(this, "活动名称不能为空!"); return; }

        string IDs = string.Empty;//商品ID字符串
        if (hd_SPDMS.Value.Trim() != string.Empty)
        {
            IDs = hd_SPDMS.Value.Substring(0, hd_SPDMS.Value.Length - 1);
        }

        if (ddl_Range.SelectedValue == "3" && IDs.Trim().Length == 0 && grdShowProduct.Rows.Count < 1)
        { Share.Alert(this, "请选择商品!"); return; }

        decimal resultCheck = 0.00M;
        decimal zk = decimal.TryParse(txt_ZK.Text, out resultCheck) ? resultCheck : 0.00M;
        if (ddl_Way.SelectedValue == "1" && zk <= 0)
        { Share.Alert(this, "请输入正确的折扣!"); return; }

        decimal je = decimal.TryParse(txt_JE.Text, out resultCheck) ? resultCheck : 0.00M;
        if (ddl_Way.SelectedValue == "2" && je <= 0)
        { Share.Alert(this, "请输入正确的金额!"); return; }

        string giftIDs = string.Empty;//礼品ID字符串
        if (hd_GIFTS.Value.Trim() != string.Empty)
        {
            giftIDs = hd_GIFTS.Value.Substring(0, hd_GIFTS.Value.Length - 1);
        }
        int slResult;
        int giftSL = int.TryParse(txt_SL.Text, out slResult) ? slResult : 0;
        if (ddl_Way.SelectedValue == "3")
        {
            if (giftIDs.Trim().Length < 1 && grdShowGift.Rows.Count < 1)
            { Share.Alert(this, "请选择礼品!"); return; }
            if (giftSL <= 0)
            { Share.Alert(this, "赠送数量不能少于1个!"); return; }
        }
        #endregion
        try
        {
            if (this.IsAdd)
            {
                Model.CXFAD8 cxfad8 = new Model.CXFAD8();
                cxfad8.DJBH = lbl_Promotion.Text;
                cxfad8.RQ = DateTime.Now;
                cxfad8.HDMC = txt_ActiveName.Text;
                cxfad8.RQ_S = DateTime.Parse(Text1.Value);
                cxfad8.RQ_E = DateTime.Parse(Text2.Value);
                cxfad8.BYZD3 = int.Parse(ddl_Range.SelectedValue);
                int value;
                cxfad8.BYZD1 = int.TryParse(ddl_Type.SelectedValue, out value) ? value : 0;
                cxfad8.BYZD4 = int.TryParse(ddl_Way.SelectedValue, out value) ? value : 0;
                Decimal result;
                cxfad8.BYZD5 = Decimal.TryParse(txt_ZK.Text, out result) ? result : 0.00M;
                cxfad8.BYZD6 = Decimal.TryParse(txt_JE.Text, out result) ? result : 0.00M;

                cxfad8.BYZD7 = int.TryParse(txt_SL.Text, out value) ? value : 0;
                cxfad8.BYZD8 = Convert.ToInt32(chkZK.Checked);
                cxfad8.BYZD9 = ddl_PinPai.SelectedValue;
                cxfad8.BZ = Tbx_Remark.Value;
                cxfad8.JZ = "0";
                cxfad8.SP = "0";
                cxfad8.RUN = "0";
                cxfad8.BYZD2 = 2;//初始 默认为未审批状态

                if (new BLL.CXFAD8().Add(cxfad8).Length > 0
                     && new BLL.CXFAD8().UpdateCXFAD8(cxfad8.DJBH, IDs, giftIDs, cxfad8.BYZD3, cxfad8.BYZD1, cxfad8.BYZD9, cxfad8.BYZD4, cxfad8.BYZD5, cxfad8.BYZD6, cxfad8.BYZD7) > 0)
                {

                    //todo: 修改的时候绑定商品是从商品明细表中去取得
                    if (ddl_Range.SelectedValue == "3")
                    {
                        BindCXFADMX1();
                    }
                    if (ddl_Way.SelectedValue == "3")
                    {
                        BindCXFADMX2();
                    }
                    //Share.Alert(this, "添加成功");
                }
            }
            else
            {
                Model.CXFAD8 cxfad8 = new BLL.CXFAD8().GetModel(this.Code);
                cxfad8.HDMC = txt_ActiveName.Text;
                cxfad8.RQ_S = DateTime.Parse(Text1.Value);
                cxfad8.RQ_E = DateTime.Parse(Text2.Value);
                cxfad8.BYZD3 = int.Parse(ddl_Range.SelectedValue);
                int value;
                cxfad8.BYZD1 = int.TryParse(ddl_Type.SelectedValue, out value) ? value : 0;
                cxfad8.BYZD4 = int.TryParse(ddl_Way.SelectedValue, out value) ? value : 0;
                Decimal result;
                cxfad8.BYZD5 = Decimal.TryParse(txt_ZK.Text, out result) ? result : 0.00M;
                cxfad8.BYZD6 = Decimal.TryParse(txt_JE.Text, out result) ? result : 0.00M;

                cxfad8.BYZD7 = int.TryParse(txt_SL.Text, out value) ? value : 0;
                cxfad8.BYZD8 = int.TryParse(chkZK.Checked.ToString(), out value) ? value : 0;
                cxfad8.BYZD9 = ddl_PinPai.SelectedValue;
                cxfad8.BZ = Tbx_Remark.Value;
                if (new BLL.CXFAD8().Update(cxfad8) == true
                     && new BLL.CXFAD8().UpdateCXFAD8(cxfad8.DJBH, IDs, giftIDs, cxfad8.BYZD3, cxfad8.BYZD1, cxfad8.BYZD9, cxfad8.BYZD4, cxfad8.BYZD5, cxfad8.BYZD6, cxfad8.BYZD7) > 0)
                {

                    //todo: 修改的时候绑定商品是从商品明细表中去取得
                    if (ddl_Range.SelectedValue == "3")
                    {
                        BindCXFADMX1();
                    }
                    if (ddl_Way.SelectedValue == "3")
                    {
                        BindCXFADMX2();
                    }
                    //Share.Alert(this, "更新成功");
                }
            }
        }
        catch (Exception ee)
        {
            Share.Alert(this, "操作失败"); return;
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (txt_SPDM.Text.Trim().Length > 1 || txt_SPMC.Text.Trim().Length > 1)
        {
            this.grdProduct.DataSource = new BLL.CXFAD8().GetShangpinByCondition(txt_SPDM.Text.Trim(), txt_SPMC.Text.Trim()).Tables[0];
            this.grdProduct.DataBind();
        }
        else
        {
            BindProduct(-1, 0, 1, "-1", "-1");
        }
    }
    protected void grdShowProduct_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            string spdm = e.CommandArgument.ToString();
            new BLL.CXFAD8MX1().Delete(lbl_Promotion.Text.Trim(), spdm);
            BindCXFADMX1();
        }
        catch (Exception ee)
        {
            Share.Alert(this, "操作失败");
            return;
        }

    }
    protected void grdShowGift_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            string spdm = e.CommandArgument.ToString();
            new BLL.CXFAD8MX2().Delete(lbl_Promotion.Text.Trim(), spdm);
            BindCXFADMX2();
        }
        catch (Exception ee)
        {
            Share.Alert(this, "操作失败");
            return;
        }

    }
    protected void grdShowProduct_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (this.Status != 2)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ((LinkButton)e.Row.FindControl("lbl_DelPro")).Enabled = false;
                ((LinkButton)e.Row.FindControl("lbl_DelPro")).OnClientClick = "return false;";
            }
        }
    }
    protected void grdShowGift_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (this.Status != 2)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ((LinkButton)e.Row.FindControl("lbl_DelGift")).Enabled = false;
                ((LinkButton)e.Row.FindControl("lbl_DelGift")).OnClientClick = "return false;";
            }
        }

    }
    protected void btn_SubPro_Click(object sender, EventArgs e)
    {
        SubmitData();
    }
    protected void btn_SubGift_Click(object sender, EventArgs e)
    {
        SubmitData();
    }
}
